﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Analyse : System.Web.UI.Page
{
    string tableName = "YCMS" +Convert.ToString( DateTime.Now.Year);
    string Month = Convert.ToString(DateTime.Now.Month);
    string Year = Convert.ToString(DateTime.Now.Year);
    DBOperation db = new DBOperation();
    int AllDepCount = 0;
    int AllDoneCount = 0;
    int NoDoneAllCount = 0;
    int roleID = 2;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] != null)
        {
            if (!IsPostBack)
            {
                BindAnalyseDate();
                lblMsg.Text = Month ;
                BindYear();
                roleID =Convert.ToInt32( Session["roleID"]);
                if(roleID==1)
                {
                    btnExport.Visible = true;
                }
            }
        }
        else 
        {
            Response.Redirect("Longin.aspx");
        }

    }
    //绑定统计数据
    protected void BindAnalyseDate()
    {
        Department dp = new Department();
        dgResult.DataSource = dp.FindDep();
        dgResult.DataBind();
    }
    //统计方法
    protected void dgResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        
       switch(e.Item.ItemType)
       {
           case ListItemType.AlternatingItem:
           case ListItemType.Item:

               string strSql = "select count(*) from " + tableName + " where DEPID=" + Convert.ToInt32(e.Item.Cells[0].Text) + " and MONTH(ADDDATE)=" + Month + " and YEAR(ADDDATE)="+Year;
               int CountAll =Convert.ToInt32( db.ExcuteScalar(strSql));
               Label lblAllCount=(Label)e.Item.Cells[2].FindControl("lblAllCount");
               lblAllCount.Text = CountAll.ToString();
               string strSqlDone = "select count(*) from " + tableName + " where DEPID=" + Convert.ToInt32(e.Item.Cells[0].Text) + " and STATUS=1 and MONTH(ADDDATE)=" + Month + " and YEAR(ADDDATE)=" + Year;
               int countDone = Convert.ToInt32(db.ExcuteScalar(strSqlDone));
               Label lblDoneCount = (Label)e.Item.Cells[3].FindControl("lblDoneCount");
               lblDoneCount.Text = countDone.ToString();
               int NoFinishCount = CountAll - countDone;
               Label lblNoFinishCount = (Label)e.Item.Cells[4].FindControl("lblNoFinishCount");
               lblNoFinishCount.Text = NoFinishCount.ToString();

               AllDepCount = AllDepCount + CountAll;
               AllDoneCount = AllDoneCount + countDone;
               NoDoneAllCount = NoDoneAllCount + NoFinishCount;

               break;
           case ListItemType.Footer:
               Label lblAllDepCount = (Label)e.Item.Cells[2].FindControl("lblAllDepCount");
               Label lblAllDoneCount = (Label)e.Item.Cells[3].FindControl("lblAllDoneCount");
               Label lblNoFinishAllCount = (Label)e.Item.Cells[4].FindControl("lblNoFinishAllCount");

               lblAllDepCount.Text =lblAllDepCount.Text+ AllDepCount.ToString();
               lblAllDoneCount.Text =lblAllDoneCount.Text+ AllDoneCount.ToString();
               lblNoFinishAllCount.Text = lblNoFinishAllCount.Text+NoDoneAllCount.ToString();
               break;

       }
       
    }
    //该重写方法不可以删除！！！！！！！
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    //导出excel
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string Month = DateTime.Now.Month.ToString();
        string FileName = Month + "月份异常统计";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        dgResult.RenderControl(hw);

        Response.Clear();
        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
        Page.EnableViewState = false;

        Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
        Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html\"></head><body><center>");
        Response.Write(sw.ToString());
        Response.Write("</center></body></html>");
        Response.End();
    }
    //选择月份查询
    protected void ddlMoth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Month = ddlMoth.SelectedValue;
        Year = ddlYear.SelectedItem.Text;
        lblMsg.Text = Month ;
        BindAnalyseDate();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("YCList.aspx");
    }
    //绑定年份
    public void BindYear()
    {
        string strSelYear = "select DATAYEAR from DATAYEAR order by AUTOID desc";
        SqlDataReader sdr = db.GetDataReader(strSelYear);
        ddlYear.DataSource = sdr;
        ddlYear.DataTextField = "DATAYEAR";
        ddlYear.DataBind();
        sdr.Close();
    }
}
